package com.ex02.ylw;

import com.util.BufferSize;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;

/**
 * 暂时自己写parse方法用来处理Url
 * Created by yangliwei on 2017/7/21.
 */
public class Request implements ServletRequest {


    private String uri;

    /**
     * 解析传进来的InputStream 资源
     * 病解析成字符串
     *
     * @param in
     */
    public void parse(InputStream in) {
        byte[] bytes = new byte[BufferSize.REQUEST_BUFFER_SIZE.getSize()];
        StringBuilder sb = new StringBuilder();
        int i;
        try {
            //这里目前还是只读一行，主要用来解析请求的 GET /index.html HTTP/1.1 参数
            i = in.read(bytes, 0, BufferSize.REQUEST_BUFFER_SIZE.getSize());
//            这里需要把字节转化为字符，要不然后面的空格这些都是数字
            for (int j = 0; j < i; j++) {
                sb.append((char)bytes[j]);
            }
            parseUri(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 从字符串头信息获取url
     *
     * @return
     */
    private void parseUri(String resource) {
        int i, j;
        i = resource.indexOf(" ");
        if (i == -1) {
            return;
        }
        j = resource.indexOf(" ", i + 1);
        if (i < j) {
            uri = resource.substring(i + 1, j);
        }

    }


    public String getUri() {
        return uri;
    }


    @Override
    public Object getAttribute(String s) {
        return null;
    }

    @Override
    public Enumeration getAttributeNames() {
        return null;
    }

    @Override
    public String getCharacterEncoding() {
        return null;
    }

    @Override
    public void setCharacterEncoding(String s) throws UnsupportedEncodingException {

    }

    @Override
    public int getContentLength() {
        return 0;
    }

    @Override
    public String getContentType() {
        return null;
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        return null;
    }

    @Override
    public String getParameter(String s) {
        return null;
    }

    @Override
    public Enumeration getParameterNames() {
        return null;
    }

    @Override
    public String[] getParameterValues(String s) {
        return new String[0];
    }

    @Override
    public Map getParameterMap() {
        return null;
    }

    @Override
    public String getProtocol() {
        return null;
    }

    @Override
    public String getScheme() {
        return null;
    }

    @Override
    public String getServerName() {
        return null;
    }

    @Override
    public int getServerPort() {
        return 0;
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return null;
    }

    @Override
    public String getRemoteAddr() {
        return null;
    }

    @Override
    public String getRemoteHost() {
        return null;
    }

    @Override
    public void setAttribute(String s, Object o) {

    }

    @Override
    public void removeAttribute(String s) {

    }

    @Override
    public Locale getLocale() {
        return null;
    }

    @Override
    public Enumeration getLocales() {
        return null;
    }

    @Override
    public boolean isSecure() {
        return false;
    }

    @Override
    public RequestDispatcher getRequestDispatcher(String s) {
        return null;
    }

    @Override
    public String getRealPath(String s) {
        return null;
    }
}
